package cn.shyouchi.wuguanjia.android.api.meet;

import android.content.Context;

import com.alibaba.fastjson.JSON;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.shyouchi.wuguanjia.android.api.ClientProvider;
import cn.shyouchi.wuguanjia.android.api.UserStorageProvider;
import cn.shyouchi.wuguanjia.android.components.storage.UserStorage;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.model.BaseResult;
import cn.shyouchi.wuguanjia.android.ui.meetting.common.MeetPerson;
import cn.shyouchi.wuguanjia.android.ui.meetting.common.MeetRoom;
import cn.shyouchi.wuguanjia.android.ui.meetting.feedback.FeedbackDetailEntity;
import cn.shyouchi.wuguanjia.android.ui.meetting.feedback.FeedbackEntity;
import cn.shyouchi.wuguanjia.android.ui.meetting.plan.PlanMeetDetailEntity;
import cn.shyouchi.wuguanjia.android.ui.meetting.plan.PlanMeetEntity;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import rx.Observable;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import top.zibin.luban.Luban;

/**
 * Created by Eenie on 2017/2/13 at 9:11
 * Email: 472279981@qq.com
 * Des:
 */

public class MeetApi {


    private MeetService mService;

    private UserStorage mUserStorage;
    private Context mContext;

    public MeetApi(Context context) {
        mUserStorage = UserStorageProvider.Default();
        mContext = context;
        Retrofit retrofit = ClientProvider.getDefaultClient().getRetrofit();
        mService = retrofit.create(MeetService.class);
    }


    public Observable<BaseRequestResult<List<MeetPerson>>> fetchCrew() {
        return mService.fetchCrew(mUserStorage.getToken());
    }

    public Observable<BaseRequestResult<List<MeetPerson>>> fetchDepartment() {
        return mService.fetchDepartment(mUserStorage.getToken());
    }

    public Observable<BaseRequestResult<List<FeedbackEntity>>> fetchRoomList() {
        return mService.fetchRoomList(mUserStorage.getToken());
    }

    public Observable<BaseResult<List<PlanMeetEntity>>> fetchMeets(String path) {
        return mService.fetchMeets(mUserStorage.getToken(), path);
    }


    public Observable<BaseRequestResult<String>> meetAdd(Map<String, Object> params) {
        return mService.meetAdd(mUserStorage.getToken(), JSON.toJSONString(params));
    }

    public Observable<BaseRequestResult<String>> meetAplly(Map<String, Object> params) {
        return mService.meetAplly(mUserStorage.getToken(), JSON.toJSONString(params));
    }


    public Observable<BaseRequestResult<List<MeetRoom>>> fetchMeetRoom(Map<String, Object> params) {
        return mService.fetchMeetRoom(mUserStorage.getToken(), params);
    }

    public Observable<BaseRequestResult<FeedbackDetailEntity>> fetchMeetRoomInfo(int roomId) {
        return mService.fetchMeetRoomInfo(mUserStorage.getToken(), roomId);
    }

    public Observable<BaseRequestResult<PlanMeetDetailEntity>> fetchMeetInfo(int meetId) {
        return mService.fetchMeetInfo(mUserStorage.getToken(), meetId);
    }
    public Observable<BaseRequestResult<String>> MeetCheckIn(int meetId) {
        return mService.MeetCheckIn(mUserStorage.getToken(), meetId);
    }
    public Observable<BaseRequestResult<List<MeetPerson>>> fetchMeetCheckPerson(int meetId) {
        return mService.fetchMeetCheckPerson(mUserStorage.getToken(), meetId);
    }


    public Observable<BaseRequestResult<String>> addMeetRecord(String id, String recordContent, ArrayList<String> imgs) {

        final Map<String, RequestBody> paramsMap = new HashMap<>();
        final List<MultipartBody.Part> partHashMap = new ArrayList<>();
        final RequestBody projectsIdBody = RequestBody.create(MediaType.parse("multipart/form-data"), id);
        paramsMap.put("id", projectsIdBody);
        final RequestBody nameBody = RequestBody.create(MediaType.parse("multipart/form-data"), recordContent);
        paramsMap.put("record", nameBody);
        return Observable.from(imgs)
                .subscribeOn(Schedulers.io())
                .flatMap(new Func1<String, Observable<File>>() {
                    @Override
                    public Observable<File> call(String path) {
                        return Luban.get(mContext)
                                .load(new File(path))
                                .putGear(Luban.THIRD_GEAR)
                                .asObservable();
                    }
                })
                .map(new Func1<File, MultipartBody.Part>() {
                    @Override
                    public MultipartBody.Part call(File file) {
                        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        MultipartBody.Part body = MultipartBody.Part.createFormData("image[]", file.getName(), requestFile);
                        return body;
                    }
                })
                .toList()
                .flatMap(new Func1<List<MultipartBody.Part>, Observable<BaseRequestResult<String>>>() {
                    @Override
                    public Observable<BaseRequestResult<String>> call(List<MultipartBody.Part> parts) {
                        partHashMap.addAll(parts);
                        return mService.addMeetRecord(mUserStorage.getToken(), paramsMap, partHashMap);
                    }
                });


    }




}
